
package com.kugou.game.sdk.ui.fragment;

import com.kugou.game.sdk.adapter.GiftBagCollectAdapter;
import com.kugou.game.sdk.adapter.GiftBagCollectAdapter.GiftBtnClickListener;
import com.kugou.game.sdk.api.common.User;
import com.kugou.game.sdk.base.BaseListAdapter;
import com.kugou.game.sdk.base.BaseListFragment;
import com.kugou.game.sdk.core.GameManager;
import com.kugou.game.sdk.core.SDKActions;
import com.kugou.game.sdk.core.UserManager;
import com.kugou.game.sdk.entity.GiftBag;
import com.kugou.game.sdk.entity.GiftbagTakenListResponse;
import com.kugou.game.sdk.ui.widget.TipsLayout;
import com.kugou.game.sdk.utils.Rx;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.text.ClipboardManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import java.util.ArrayList;

/**
 * 描述:已领取礼包界面
 * 
 * @author liux
 * @since 2014-8-17 上午09:54:24
 */
@SuppressLint("NewApi")
public class TakenGiftbagFragment extends BaseListFragment<GiftBag> implements GiftBtnClickListener {

    private GiftBagCollectAdapter mAdapter;

    public static TakenGiftbagFragment newInstance() {
        TakenGiftbagFragment fragment = new TakenGiftbagFragment();
        return fragment;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }

    @Override
    public void setupActions(ArrayList<String> actions) {
        actions.add(SDKActions.ACTION_GET_NEW_GIFT_BAG);
    }

    @Override
    public void handleBroadcast(Context context, Intent intent) {
        super.handleBroadcast(context, intent);
        String action = intent.getAction();
        if (SDKActions.ACTION_GET_NEW_GIFT_BAG.equals(action)) {
            reLoadData();
        }

    }

    View emptyView;

    /**
     * 初始化界面信息
     */
    private void initView() {
        mAdapter.setGiftBtnClickListener(this);
        emptyView = LayoutInflater.from(getActivity()).inflate(
                Rx.layout.kg_layout_load_empty_content, null);
    }

    @Override
    protected int findLayoutId() {
        return Rx.layout.kg_fragment_giftbag_listview;
    }

    @Override
    protected BaseListAdapter<GiftBag> createAdapter() {
        mAdapter = new GiftBagCollectAdapter(getActivity(), mListView);
        return mAdapter;
    }

    @Override
    protected ArrayList<GiftBag> loadDatas() {
        User user = UserManager.getInstance().getCurrentUserInternal();
        if (user == null) {
            return null;
        }

        GiftbagTakenListResponse response = GameManager.requestTakenGiftBagList(getPageIndex(),
                PAGE_SIZE, user.getAccountName());
        if (response != null && response.isOk()) {
            return response.getGiftbagList();
        } else {
            return null;
        }
    }

    @Override
    public void handleUiMessage(Message msg) {
        super.handleUiMessage(msg);
        if (getActivity() == null) {
            return;
        }
        switch (msg.what) {
            case MSG_UI_NO_DATA:
                mTipsLayout.setCustomView(emptyView);
                mTipsLayout.show(TipsLayout.TYPE_CUSTOM_VIEW);
                break;
            default:
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onGiftBtnClickListener(View v, GiftBag info) {
        if (!TextUtils.isEmpty(info.getExistscardnumber())) {// 复制礼包
            copy(info.getExistscardnumber(), getActivity());
            Toast.makeText(getActivity(), "复制成功", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 实现文本复制功能
     * 
     * @param content
     */
    public void copy(String content, Context context) {
        // 得到剪贴板管理器
        ClipboardManager cmb = (ClipboardManager) context
                .getSystemService(Context.CLIPBOARD_SERVICE);
        cmb.setText(content.trim());
    }
}
